package net.lwf.wp.fake.client;

import java.util.List;
import java.util.Random;

import net.lwf.wp.fake.jaxb.Data;
import net.lwf.wp.fake.service.MockWpDataService;

public class WpDataClient {
	
	private static int highest = 0; 

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		generateDataSQL("0", "arithmatics");
		generateDataSQL("1", "arithmatics");
		generateDataSQL("2", "arithmatics");
		generateDataSQL("2", "time");
		generateDataSQL("3", "arithmatics");
		generateDataSQL("3", "time");
		generateDataSQL("4", "arithmatics");
		generateDataSQL("4", "time");
		
		System.out.println("===================> Max char in the problem: " + highest);
		
		String grade = "2";
		String type = "arithmatics";
		
		Data data = getSingleData(grade, type);
		System.out.println("=============== Reading Single Data: Grade: " + grade + "; Type: " + type + " ==================");
		print(data);
		
		grade = "2";
		type = "time";
		List<Data> datas= getMultipleData(grade, type);
		System.out.println("=============== Reading Multiple Data: Grade: " + grade + "; Type: " + type + " ==================");
		for(Data idata: datas) {
			print(idata);
		}

	}
	
	private static Data getSingleData(String grade, String type) {
		MockWpDataService service = new MockWpDataService();
		List<Data> data = service.getMockData(grade, type);
		int rand = new Random().nextInt(data.size());
		return data.get(rand);
	}
	
	private static List<Data> getMultipleData(String grade, String type) {
		MockWpDataService service = new MockWpDataService();
		return service.getMockData(grade, type);
	}
	
	private static void print(Data data) {
		System.out.println("ID: " + data.getId());
		System.out.println("Class Name: " + data.getClassName());
		System.out.println("Replacements: " + data.getReplacements());
		System.out.println("Problem : " + data.getProblem() + "\n");
	}
	
	//INSERT INTO mtb_user (user_id, password, user_status, first_name, last_name) VALUES('', 'CzE+ir+m7fmJoJdOKnVXfCh5y6+yXnBc', 'ACTIVE', '', '');
/*	CREATE TABLE IF NOT EXISTS `word_problem` (
			  `id` bigint(20) unsigned NOT NULL auto_increment,
			  `wp_grade` varchar(5) NOT NULL,
			  `wp_type` varchar(20) NOT NULL,
			  `wp_classname` varchar(100) NOT NULL,
			  `wp_problem` varchar(500) NOT NULL,
			  `wp_replacements` varchar(20) NOT NULL,
			  `wp_datainput` varchar(40) NOT NULL,
			  `wp_remarks` varchar(20) NOT NULL,
			  PRIMARY KEY (`id`)
			) ENGINE=InnoDB DEFAULT CHARSET=latin1;*/
	
	private static void generateDataSQL(String grade, String type) {
		String insert_stmt = "INSERT INTO word_problem (wp_grade, wp_type, wp_classname, wp_problem, wp_replacements, wp_datainput, wp_remarks) VALUES ('" + grade + "', '" + type + "', ";
		MockWpDataService service = new MockWpDataService();
		List<Data> datas = service.getMockData(grade, type);
		for(Data data: datas) {
			int problenght = data.getProblem().length();
			if(highest < problenght) {
				highest = problenght;
			}
			String fullStmt = "";
			fullStmt += insert_stmt;
			fullStmt += "'";
			fullStmt += data.getClassName();
			fullStmt += "', '";
			fullStmt += data.getProblem();
			fullStmt += "', '";
			fullStmt += data.getReplacements();
			fullStmt += "', '";
			if(data.getInput() == null) {
				fullStmt += "";
			}
			else {
				fullStmt += data.getInput();
			}
			fullStmt += "', '";
			if(data.getRemarks() == null) {
				fullStmt += "";
			}
			else {
				fullStmt += data.getRemarks();
			}
			
			fullStmt += "');";
			System.out.println(fullStmt);
		}
	}

}
